package com.Dubbo.test.web.controller.demo;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.Dubbo.api.test.demo.entity.Demo;
import com.Dubbo.api.test.demo.service.IDemoService;
import com.alibaba.dubbo.config.annotation.Reference;

/**
*
* dubbo 消费者
*
* @Reference 注解需要在 dubbo consumer中做如下配置
*
* <dubbo:annotation/>
*	<context:component-scan base-package="net.aimeizi.dubbo.controller">
*	<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Reference"/>
* </context:component-scan>
*
* 若要使用@Autowired或@Resource注解需要显式声明bean
*
* 使用@Autowired或@Resource注解时需要使用dubbo:reference来声明
* <dubbo:reference interface="net.aimeizi.dubbo.service.UserService" id="userService"/>
* <dubbo:reference interface="net.aimeizi.dubbo.service.DemoService" id="demoService"/>
*
* 以上的配置均需要在spring mvc的DispatcherServlet配置中显式配置dubbo consumer的配置.如/WEB-INF/applicationContext-dubbo-consumer.xml 否则在Controller中服务报NullPointException
* <servlet>
*	<servlet-name>mvc-dispatcher</servlet-name>
*		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
*	<init-param>
*	<param-name>contextConfigLocation</param-name>
*		<param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
*	</init-param>
*	<load-on-startup>1</load-on-startup>
* </servlet>
*
*/
@Controller
@RequestMapping(value="")
public class DemoController {
	
	@Reference
	private IDemoService demoService;

	@RequestMapping(value = "/index")
	public ModelAndView home(ModelAndView model, HttpServletRequest request) {
		String dubboTestString = demoService.selectNameByPrimaryKey(55);
		model.addObject("dubboTestString", dubboTestString);
		model.setViewName("index/index");
		return model;
	}
	
	@RequestMapping(value = "/save")
	public ModelAndView save(ModelAndView model) {
		Demo demo = new Demo();
		demo.setCreateTime(new Date());
		demo.setDeleteFlag(0);
		demo.setName(new Date().toString());
		
		int demoId = demoService.insertEntity(demo);
		
		model.addObject("demo", demo);
		model.addObject("demoId", demoId);
		
		model.setViewName("index/save");
		return model;
	}
}
